﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using ClientWPF.BankService;

namespace ClientWPF
{
    /// <summary>
    /// Interaction logic for StatusesPopup.xaml
    /// </summary>
    public partial class StatusesPopup
    {

        private readonly BankServiceClient bankServiceClient;
        private string clientName;
        private readonly string clientUnp;
        private Guid clientId;

        public PaymentOrder PaymentOrderForCopy { set; get; }

        public StatusesPopup()
        {
            InitializeComponent();
        }

        public StatusesPopup(BankServiceClient bankServiceClient, string clientName, string clientUnp, Guid clientId)
            : this()
        {
            this.bankServiceClient = bankServiceClient;
            this.clientName = clientName;
            this.clientUnp = clientUnp;
            this.clientId = clientId;
        }

        private void BtnGetPoListClick(object sender, RoutedEventArgs e)
        {
            DateTime dateFrom;
            DateTime dateTo;
            int numb;

            DateTime.TryParse(deDateFrom.Text, out dateFrom);
            DateTime.TryParse(deDateTo.Text, out dateTo);
            int.TryParse(tePoNumber.Text, out numb);
            IEnumerable<PaymentOrder> poLst = bankServiceClient.GetPaymentOrders(clientUnp, dateFrom, dateTo, numb).OrderByDescending(x => x.Date);
            gridControlPaymentOrders.DataContext = poLst;
            //dgPoTable.Items.Clear();
            //dgPoTable.Columns.Clear();
            //dgPoTable.DataContext = poLst;
            //dgPoTable.ItemsSource = poLst;
            //foreach (var po in poLst)
            //{
            //    dgPoTable.Items.Add(new PaymentOrderTableItem { Number = po.Number.ToString(), Date = po.Date.ToString(), IsUrgent = po.Urgent ? "Да" : "Нет", State = po.State.TransactionState, Sum = po.Sum.ToString(("F2")), CurrencyCode = po.Currency.ToString() });
            //}

            //dgPoTable.Columns.Add(new DataGridTextColumn { Header = "Номер", Binding = new Binding("Number"), IsReadOnly = true });
            //dgPoTable.Columns.Add(new DataGridTextColumn { Header = "Срочное", Binding = new Binding("Urgent"), IsReadOnly = true });
            //dgPoTable.Columns.Add(new DataGridTextColumn { Header = "Дата", Binding = new Binding("Date"), IsReadOnly = true });
            //dgPoTable.Columns.Add(new DataGridTextColumn { Header = "Статус", Binding = new Binding("State.TransactionState"), IsReadOnly = true });
            //dgPoTable.Columns.Add(new DataGridTextColumn { Header = "Сумма", Binding = new Binding("Sum"), IsReadOnly = true });
            //dgPoTable.Columns.Add(new DataGridTextColumn { Header = "Код валюты", Binding = new Binding("Currency"), IsReadOnly = true });
            //TOL: если хочешь добавить колонку, то не забудь изменить на стороне сервиса public IEnumerable<PaymentOrder> GetPaymentOrders(string clientUnp, DateTime dateFrom, DateTime dateTo, int number),
            //а то колонка будет содержать только начальное значение
        }

        protected class PaymentOrderTableItem
        {
            public string Number { set; get; }
            public string Date { set; get; }
            public string State { set; get; }
            public string IsUrgent { set; get; }
            public string Sum { set; get; }
            public string CurrencyCode { set; get; }
        }

        private void GridControlPaymentOrdersMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            PaymentOrderForCopy = (PaymentOrder) gridControlPaymentOrders.View.FocusedRow;
            Close();
        }
    }
}